#include <stdio.h>

void select_sort(int *arrary, int item_num, int is_asc)
{
    int now_index = 0;
    int  i = 0;
    int max_index = 0;
    int temp_value = 0;
    
    for (now_index = 0;  now_index < item_num; now_index++ ) {
        max_index = now_index;
        for (i = now_index + 1; i< item_num; i++) {
            if (is_asc) {
                if (arrary[i] <= arrary[max_index]) {
                    printf("[%d] value: %d,  [%d]value: %d\n", i, arrary[i], max_index, arrary[max_index]);
                    max_index = i;
                }
            } else {
                if (arrary[i] >= arrary[max_index]) {
                    printf("[%d] value: %d,  [%d]value: %d\n", i, arrary[i], max_index, arrary[max_index]);
                    max_index = i;
                }
            }
        }
        if (is_asc) {
            printf("get min value [%d]:%d\n", max_index, arrary[max_index]);
        } else {
            printf("get max value [%d]:%d\n", max_index, arrary[max_index]);
        }

        temp_value = arrary[now_index];
        arrary[now_index] = arrary[max_index];
        arrary[max_index] = temp_value; 
        max_index = now_index+1;
    }
    
    printf("insert sort result: ");
    for (i = 0; i < item_num; i++) {
        printf("%d\t", arrary[i]);
    }
    printf("\n");
    return;
}

int main()
{
    int arrary[10] = {5, 4, 3, 2, 1, 9, 8, 7, 6, 10};
    int arrary2[10] = {5, 2, 3, 2, 1, 10, 8, 7, 6, 10};
    int item_num = sizeof(arrary)/sizeof(int);
    
    printf("=============================================\n");
    select_sort(arrary, item_num, 1);
    printf("=============================================\n");
    select_sort(arrary2, item_num, 0);
    
    return 0;
}